﻿using UnityEngine;

public class SingletonManager<T> : MonoBehaviour where T : MonoBehaviour
{
    private static T instance;
    public static T Instance
    {
        get
        {
            if(instance==null)
            {
                instance=GameObject.FindObjectOfType<T>();
                if(instance==null)
                {
                    GameObject newObj = new GameObject(typeof(T).Name);
                    newObj.AddComponent<T>();
                }
            }

            return instance;
        }
    }

    public void Awake()
    {
        if(instance==null)
        {
            instance=this as T;
        }
        else if(instance!=this)
        {
            Destroy(gameObject);
        }
    }
}